package com.xb.p36_mediavideo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    SurfaceView surfaceView;
    Button button;

    MediaPlayer player;
    SurfaceHolder holder;

    String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView=findViewById(R.id.scw_play);
        button=findViewById(R.id.btn_play);
        player=new MediaPlayer();
        path="/sdcard/Movies/大气.mp4";

        /*if(Build.VERSION.SDK_INT>=23){
            int permission= ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
            if(permission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},0);
            }
        }*/

        checkNeedPermissions();

    }

    //权限申请
    private void checkNeedPermissions(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //多个权限一起申请
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            }, 1);
        }
    }

    //权限申请回调函数，查看权限申请否真的成功
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(MainActivity.this,permissions[i] + "未授权",Toast.LENGTH_SHORT).show();
                    continue;
                }
                Toast.makeText(MainActivity.this,permissions[i] + "已授权",Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void video_play(View view){

        try {
            player.reset();
            player.setDataSource(path);
            if(Build.VERSION.SDK_INT>=23){
                AudioAttributes attr=new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
                        .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED).build();
                player.setAudioAttributes(attr);
            }else {
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            }
            holder=surfaceView.getHolder();
            player.setDisplay(holder);
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}